//
//  MineCenterFooterView.m
//  MoXiProject
//
//  Created by 周筱雯 on 2017/7/19.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "MineCenterFooterView.h"
#define height_button 8 // btn 减去的高度
@interface MineCenterFooterView(){
    UIButton *btn; UIImageView *imageView;
}
@property (nonatomic,strong) NSMutableArray * dataArrayList;
@property (nonatomic,strong) UIView * bgView;
@end

@implementation MineCenterFooterView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor whiteColor]];
        
        [self initUI];
    }
    return self;
}

-(NSMutableArray *) dataArrayList{
    if (!_dataArrayList) {
        
        _dataArrayList = [[NSMutableArray alloc] initWithObjects:@"彩票收藏",@"新闻收藏",@"身份验证",@"安全信息",@"我参与的活动" ,@"在线客服",@"推荐专家", @"关于我们",@"联系我们",nil];
        
    }    return _dataArrayList;
}

-(void) initUI{
    
    CGFloat width = self.width;
    
    CGFloat btnW = (width - 2)/3;
    
    //得到button的总高度：：(总/每行个数)*(高度 + 分割线高度)
    self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,([self.dataArrayList count]/3)*(btnW + 1) , ([self.dataArrayList count]/3)* (btnW - height_button))];
    [self.bgView setBackgroundColor:DEF_COLOR_F0F0F0];
    
    
    for (int i=0; i<[self.dataArrayList count]; i++) {
        
        btn = [UIButton buttonWithType:UIButtonTypeCustom];
        //间隙大代码：((btnW+1)+数字)
        [btn setFrame:CGRectMake((i/[self.dataArrayList count]*width)+i%[self.dataArrayList count]%3*(btnW+1), i%[self.dataArrayList count]/3*(btnW - height_button + 1), btnW, btnW - height_button)];
        
        btn.tag = i;
        btn.backgroundColor = [UIColor whiteColor];
        [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        /**
         防止两个按钮同时点击
         */
        [btn  setExclusiveTouch :YES];
        
        // 创建imageview
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"icon_user_%d",i+1]];
        
        CGFloat jianwidth = 0;
        CGFloat jianheight = 0;
        if (DEF_DEVICE_Iphone5) {
            jianheight = 0;
            jianwidth = 3;
        }else if(DEF_DEVICE_Iphone6){
            jianheight = 5;
            jianwidth = 5;
        }else if (DEF_DEVICE_Iphone6p) {
            jianheight = 10;
            jianwidth = 8;
        }
        
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(btn.width/3 + jianwidth,btn.width/3-(btn.width/3/2) + jianheight,30,30)];
        [imageView setImage:image];
        
        // 创建label
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,imageView.frame.origin.y+imageView.height+2,btn.width,50)];
        [label setText:[self.dataArrayList objectAtIndex:i]];
        [label setNumberOfLines:0];
        [label setTextAlignment:NSTextAlignmentCenter];
//        [label setFont:[UIFont systemFontOfSize:14.f]];
        
        [label setFont:[UIFont fontWithName:@"Heiti TC" size:14.f]];
//
//        NSLog(@"%@",UIFont.familyNames);
        
        // 添加到button中
        [btn addSubview:label];
        [btn addSubview:imageView];
        [self.bgView addSubview:btn];
    }
    [self addSubview:self.bgView];
}

-(void) buttonClick:(UIButton *) btnTag{
    
    NSInteger tag = btnTag.tag +  1;
    
    if (self.buttonDelegate && [self.buttonDelegate respondsToSelector:@selector(touchCenterButtonAction:)]) {
        [self.buttonDelegate touchCenterButtonAction:tag];
    }
}

@end
